
import os
import project

def start(proj, log):
    #connect to database and pull down aliases
    #write to file projectDir/clusteroutput
    db = project.getDatabase()
    output = open(proj.projectDir + "/" + "clusterinput", 'w')
    for email, name in db.query("select distinct email, name from aliases").getresult():
        output.write("%s|%s\n" %(email, name))
    log.write("filled clusterinput file")
    #run cluster application on alias file
    
    try:
        os.system("cluster/cluster %s/clusterinput" % proj.projectDir)
    except:
        raise project.ProjectError("Could not run cluster program")

    try:
        os.system("mv pairs %s/" % proj.projectDir)
        os.system("mv imperfectPairs %s/" % proj.projectDir)
        os.system("rm cluster*.dot")
    except:
        raise project.ProjectError("problem moving files from main dir to project dir")
